#![allow(non_upper_case_globals)]
// 允许使用非大写字母开头的全局变量

use crate::cpu::cpu::reg128;
use crate::softfloat::F80;
use crate::state_flags::CachedStateFlags;

// 通用寄存器指针 (8位/16位/32位)
pub const reg8: *mut u8 = 64 as *mut u8;   // 8位寄存器指针
pub const reg16: *mut u16 = 64 as *mut u16; // 16位寄存器指针
pub const reg32: *mut i32 = 64 as *mut i32; // 32位寄存器指针

// 指令执行状态相关指针
pub const last_op_size: *mut i32 = 96 as *mut i32;      // 上一条指令的操作数大小
pub const flags_changed: *mut i32 = 100 as *mut i32;    // 标志位变化状态
pub const last_op1: *mut i32 = 104 as *mut i32;         // 上一条指令的第一个操作数
pub const state_flags: *mut CachedStateFlags = 108 as *mut CachedStateFlags; // 缓存的状态标志
pub const last_result: *mut i32 = 112 as *mut i32;      // 上一条指令的结果
pub const flags: *mut i32 = 120 as *mut i32;            // EFLAGS寄存器值

// 段访问字节指针
pub const segment_access_bytes: *mut u8 = 512 as *mut u8; // 段访问权限字节
// TODO: 重排序到 segment_limits 下方

// 页面和中断控制器相关
pub const page_fault: *mut bool = 540 as *mut bool;     // 页面错误标志

pub const apic_enabled: *mut bool = 548 as *mut bool;   // APIC (高级可编程中断控制器) 启用标志
pub const acpi_enabled: *mut bool = 552 as *mut bool;   // ACPI (高级配置和电源接口) 启用标志

// 指令指针和中断描述符相关
pub const instruction_pointer: *mut i32 = 556 as *mut i32; // 当前指令指针 (EIP)
pub const previous_ip: *mut i32 = 560 as *mut i32;         // 上一个指令指针
pub const idtr_size: *mut i32 = 564 as *mut i32;          // IDT (中断描述符表) 大小
pub const idtr_offset: *mut i32 = 568 as *mut i32;        // IDT 基址偏移
pub const gdtr_size: *mut i32 = 572 as *mut i32;          // GDT (全局描述符表) 大小
pub const gdtr_offset: *mut i32 = 576 as *mut i32;        // GDT 基址偏移
pub const cr: *mut i32 = 580 as *mut i32;                 // 控制寄存器组 (CR0-CR7)
pub const cpl: *mut u8 = 612 as *mut u8;                  // 当前特权级别
pub const in_hlt: *mut bool = 616 as *mut bool;           // HLT指令执行状态
pub const last_virt_eip: *mut i32 = 620 as *mut i32;      // 上一个虚拟EIP
pub const eip_phys: *mut i32 = 624 as *mut i32;           // EIP的物理地址

// 系统调用相关
pub const sysenter_cs: *mut i32 = 636 as *mut i32;        // SYSENTER_CS寄存器
pub const sysenter_esp: *mut i32 = 640 as *mut i32;       // SYSENTER_ESP寄存器
pub const sysenter_eip: *mut i32 = 644 as *mut i32;       // SYSENTER_EIP寄存器
pub const prefixes: *mut u8 = 648 as *mut u8;             // 指令前缀字节
pub const instruction_counter: *mut u32 = 664 as *mut u32; // 指令计数器
pub const sreg: *mut u16 = 668 as *mut u16;               // 段寄存器组
pub const dreg: *mut i32 = 684 as *mut i32;               // 调试寄存器组

// SVGA显示相关 (由svga_fill_pixel_buffer填充，供JavaScript的优化putImageData调用读取)
pub const svga_dirty_bitmap_min_offset: *mut u32 = 716 as *mut u32; // 脏位图的最小偏移
pub const svga_dirty_bitmap_max_offset: *mut u32 = 720 as *mut u32; // 脏位图的最大偏移

// 段相关
pub const segment_is_null: *mut bool = 724 as *mut bool;  // 段是否为空
pub const segment_offsets: *mut i32 = 736 as *mut i32;    // 段偏移量数组
pub const segment_limits: *mut u32 = 768 as *mut u32;     // 段界限数组

// 模式和配置相关
pub const protected_mode: *mut bool = 800 as *mut bool;   // 保护模式标志
pub const is_32: *mut bool = 804 as *mut bool;            // 32位模式标志
pub const stack_size_32: *mut bool = 808 as *mut bool;    // 32位栈大小标志
pub const memory_size: *mut u32 = 812 as *mut u32;        // 内存大小
pub const fpu_stack_empty: *mut u8 = 816 as *mut u8;      // FPU栈空标志
pub const mxcsr: *mut i32 = 824 as *mut i32;              // SSE控制和状态寄存器

// XMM和TSC相关
pub const reg_xmm: *mut reg128 = 832 as *mut reg128;      // XMM寄存器组 (128位SSE寄存器)
pub const current_tsc: *mut u64 = 960 as *mut u64;        // 当前时间戳计数器值

// 分页相关
pub const reg_pdpte: *mut u64 = 968 as *mut u64;          // 页目录指针表项 (4个64位条目)

// FPU (浮点单元) 相关
pub const fpu_stack_ptr: *mut u8 = 1032 as *mut u8;       // FPU栈指针
pub const fpu_control_word: *mut u16 = 1036 as *mut u16;  // FPU控制字
pub const fpu_status_word: *mut u16 = 1040 as *mut u16;   // FPU状态字
pub const fpu_opcode: *mut i32 = 1044 as *mut i32;        // FPU操作码
pub const fpu_ip: *mut i32 = 1048 as *mut i32;            // FPU指令指针
pub const fpu_ip_selector: *mut i32 = 1052 as *mut i32;   // FPU指令指针选择器
pub const fpu_dp: *mut i32 = 1056 as *mut i32;            // FPU数据指针
pub const fpu_dp_selector: *mut i32 = 1060 as *mut i32;   // FPU数据指针选择器
pub const tss_size_32: *mut bool = 1128 as *mut bool;     // 32位TSS (任务状态段) 标志

// SSE和FPU寄存器
pub const sse_scratch_register: *mut reg128 = 1136 as *mut reg128; // SSE临时寄存器

pub const fpu_st: *mut F80 = 1152 as *mut F80;            // FPU ST寄存器组 (80位浮点寄存器)

/// 获取32位通用寄存器的偏移量
/// 
/// # 参数
/// - `r`: 寄存器索引 (0-7)
/// 
/// # 返回值
/// 寄存器在内存中的偏移量
pub fn get_reg32_offset(r: u32) -> u32 {
    dbg_assert!(r < 8);
    (unsafe { reg32.offset(r as isize) }) as u32
}

/// 获取MMX寄存器的偏移量
/// 
/// # 参数
/// - `r`: 寄存器索引 (0-7)
/// 
/// # 返回值
/// MMX寄存器在内存中的偏移量
pub fn get_reg_mmx_offset(r: u32) -> u32 {
    dbg_assert!(r < 8);
    (unsafe { fpu_st.offset(r as isize) }) as u32
}

/// 获取XMM寄存器的偏移量
/// 
/// # 参数
/// - `r`: 寄存器索引 (0-7)
/// 
/// # 返回值
/// XMM寄存器在内存中的偏移量
pub fn get_reg_xmm_offset(r: u32) -> u32 {
    dbg_assert!(r < 8);
    (unsafe { reg_xmm.offset(r as isize) }) as u32
}

/// 获取段寄存器的偏移量
/// 
/// # 参数
/// - `s`: 段寄存器索引 (0-5)
/// 
/// # 返回值
/// 段寄存器在内存中的偏移量
pub fn get_sreg_offset(s: u32) -> u32 {
    dbg_assert!(s < 6);
    (unsafe { sreg.offset(s as isize) }) as u32
}

/// 获取段偏移量的偏移量
/// 
/// # 参数
/// - `s`: 段索引 (0-7)
/// 
/// # 返回值
/// 段偏移量在内存中的偏移量
pub fn get_seg_offset(s: u32) -> u32 {
    dbg_assert!(s < 8);
    (unsafe { segment_offsets.offset(s as isize) }) as u32
}

/// 获取段空标志的偏移量
/// 
/// # 参数
/// - `s`: 段索引 (0-7)
/// 
/// # 返回值
/// 段空标志在内存中的偏移量
pub fn get_segment_is_null_offset(s: u32) -> u32 {
    dbg_assert!(s < 8);
    (unsafe { segment_is_null.offset(s as isize) }) as u32
}

/// 获取控制寄存器的偏移量
/// 
/// # 参数
/// - `i`: 控制寄存器索引 (0-7)
/// 
/// # 返回值
/// 控制寄存器在内存中的偏移量
pub fn get_creg_offset(i: u32) -> u32 {
    dbg_assert!(i < 8);
    (unsafe { cr.offset(i as isize) }) as u32
}
